/** 
 * 	
 * name：环境判断
 * date：2020-12-14
 * author: Messi10
 * 
 */

// #ifdef H5
let userAgent = navigator.userAgent
let userAgentLowerCase = userAgent.toLowerCase()
// 安卓原生APP
const isAndroidAPP = () => userAgentLowerCase.includes('xingmugou-android') || (window.Android && window.Android.easyShow)
// IOS原生APP
const isIOSAPP = () => userAgentLowerCase.includes('xingmugou-ios') || (window.webkit && window.webkit.messageHandlers &&
	window.webkit.messageHandlers.easyShow)
// 是否小程序环境
const isMiniprogram = () => (window.__wxjs_environment === 'miniprogram') || userAgent.includes('miniProgram')
// 是否微信环境
const isWeChat = () => userAgentLowerCase.includes('micromessenger')
// 苹果桌面快捷方式
const isiOSDeskTop = () => userAgentLowerCase.includes('applewebkit') && window.navigator.standalone
// 安卓桌面安装包快捷方式
const isAndroidDeskTop = () => !!userAgent.match(/\(i[^;]+;( U;)? CPuserAgent.+Mac OS X/) && userAgent.indexOf(
	'SupperApp_Android') !== -1
// 是否苹果X手机
const isIphoneX = () => isiOS() && screen.height === 812 && screen.width === 375
// 是否android终端
const isAndroid = () => {
	if (isAndroidAPP()) return true
	return userAgent.includes('Android')
}
// 是否IOS终端
const isiOS = () => {
	if (isIOSAPP()) return true
	return !!userAgent.match(/\(i[^;]+;( U;)? CPuserAgent.+Mac OS X/)
}
// h5中android终端
var isBrowserAndroid = () => {
	return userAgent.indexOf('Android') > -1 || userAgent.indexOf('Adr') > -1;
}
// h5中ios终端
var isBrowseriOS = () => {
	return !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
}

// 处于哪个APP环境中
function isWhoApp(who) {
	if (userAgentLowerCase.includes(`xingmugou-${who}`)) {
		return 'QingYuan'
	} else if (userAgentLowerCase.includes(`huizhou-${who}`)) {
		return 'HuiZhou'
	} else {
		return 'ZhaoQing'
	}
}


function isiPhone12_2() {
	if (isiOS()) {
		let vstr = userAgentLowerCase.match(/cpu iphone os (.*?) like mac os/)
		if (!vstr) return false
		let verson = vstr[1].split('_')
		return verson[0] >= 12 && verson[1] >= 2
	} else {
		return false
	}
}

// 是否PC端
function isPC() {
	let Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod']
	let flag = true
	if (isiOS() || isAndroid()) {
		return false
	}
	for (let i = 0; i < Agents.length; i++) {
		if (userAgent.indexOf(Agents[i]) > -1) {
			flag = false
			break
		}
	}
	return flag
}

// 获取当前环境值
export const getEnv = () => {
	//默认是APP内部
	let env = []

	//PC桌面端
	if (isPC()) {
		env.push('PC')
		return env
	}

	//微信客户端
	if (isMiniprogram()) {
		env.push('MiniProgram')
	} else if (isWeChat()) {
		env.push('WeChat')
	}

	//安卓端
	if (isAndroid()) {
		env.push('Android')
		if (isAndroidAPP()) {
			env.push('AndroidAPP')
			env.push('NativeApp')
			env.push(isWhoApp('android'))
		} else if (isAndroidDeskTop()) {
			env.push('AndroidDeskTop')
		}
		return env
	}

	//苹果端
	if (isiOS()) {
		env.push('iOS')
		if (isIOSAPP()) {
			env.push('iOSAPP')
			env.push('NativeApp')
			env.push(isWhoApp('ios'))
		} else if (isiOSDeskTop()) {
			env.push('iOSDeskTop')
		} else if (isiPhone12_2()) {
			env.push('isiPhone12_2')
		}

		if (isIphoneX()) {
			env.push('IphoneX')
		}
		return env
	}
	if (isBrowserAndroid()) {
		env.push('h5Android')
	}
	if (isBrowseriOS()) {
		env.push('h5Ios')
	}
	env.push('Others')
	return env
}
// #endif
